﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;

namespace SPFluid.Core.Utilities
{
    public static partial class Map
    {
        public static SPUser UserFromDatabase(SPWeb web, object value)
        {
            if (value == null)
            {
                return null;
            }

            SPFieldUserValue fieldUserValue;
            if (value is SPFieldUserValue)
            {
                fieldUserValue = value as SPFieldUserValue;
            }
            else if (value is string)
            {
                var stringValue = value as string;
                fieldUserValue = new SPFieldUserValue(web, stringValue);
            }
            else
            {
                return null;
            }

            return EnsureUser(web, fieldUserValue);
        }

        public static IList<SPUser> UsersFromDatabase(SPWeb web, object value)
        {
            if (value == null)
            {
                return new List<SPUser>();
            }

            SPFieldUserValueCollection fieldUserValueCollection;
            if (value is SPFieldUserValueCollection)
            {
                fieldUserValueCollection = value as SPFieldUserValueCollection;
            }
            else if (value is string)
            {
                var stringValue = value as string;
                fieldUserValueCollection = new SPFieldUserValueCollection(web, stringValue);
            }
            else
            {
                return new List<SPUser>();
            }

            return fieldUserValueCollection.Select(fuv => EnsureUser(web, fuv)).Where(u => u != null).ToList();
        }

        public static SPFieldUserValue UserToDatabase(SPWeb web, SPUser user)
        {
            if (user == null)
            {
                return null;
            }

            return new SPFieldUserValue(web, user.ID, user.LoginName);
        }

        public static SPFieldUserValueCollection UsersToDatabase(SPWeb web, IEnumerable<SPUser> userCollection)
        {
            if (userCollection == null)
            {
                return null;
            }

            var mappedUserCollection = new SPFieldUserValueCollection();
            var mappedUsers = userCollection.Select(user => new SPFieldUserValue(web, user.ID, user.LoginName));
            mappedUserCollection.AddRange(mappedUsers);

            return mappedUserCollection;
        }

        private static SPUser EnsureUser(SPWeb web, SPFieldUserValue fieldUserValue)
        {
            if (string.IsNullOrEmpty(fieldUserValue.LookupValue))
            {
                return null;
            }

            if (fieldUserValue.LookupId == -1)
            {
                SPSecurity.RunWithElevatedPrivileges(() =>
                {
                    using (var elevatedSite = new SPSite(web.Url))
                    using (var elevatedWeb = elevatedSite.OpenWeb())
                    {
                        elevatedWeb.EnsureUser(fieldUserValue.LookupValue);
                    }
                });
            }

            return fieldUserValue.User;
        }
    }
}